package tech.dongliang.common.enums;

import tech.dongliang.common.error.CommonError;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public enum EBizError implements CommonError {
    LOGIN_TIMEOUT(401, "登录超时"),
    AUTH_DENIED(403, "无权限访问"),
    PARAMETER_VALIDATION_ERROR(10000, "参数错误"),
    UNKNOWE_ERROR(10001, "未知错误"),
    PASSWORD_ERROR(10002, "两次密码输入不一致"),
    LOGIN_ERROR(10003, "用户名或密码错误"),
    DEPT_HAS_CHILD(80001, "部门有子部门"),
    DEPT_HAS_USER(80002, "部门有员工"),
    MENU_HAS_CHILD(80002, "菜单有子元素"),
    UPLOAD_ERROR(90000, "上传错误");

    /**
     * 错误信息
     */
    private String errMsg;
    /**
     * 错误代码
     */
    private Integer errCode;
    private static Map<Integer, EBizError> valueMap = new HashMap<>();

    static {
        for (EBizError error : EBizError.values()) {
            valueMap.put(error.getErrCode(), error);
        }
    }

    // 普通方法
    public static EBizError getName(Integer code) {
        EBizError result = valueMap.get(code);
        if (result == null) {
            throw new IllegalArgumentException("No element matches " + code);
        }
        return result;
    }

    EBizError(Integer errCode, String errMsg) {
        this.errMsg = errMsg;
        this.errCode = errCode;
    }

    @Override
    public Integer getErrCode() {
        return this.errCode;
    }

    @Override
    public String getErrMsg() {
        return this.errMsg;
    }

    @Override
    public CommonError setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }
}
